summaryrefslogtreecommitdiff
path: root/frontend/app/api/fs/[...path]
diff options
context:
space:
mode:
Diffstat (limited to 'frontend/app/api/fs/[...path]')
-rw-r--r--frontend/app/api/fs/[...path]/route.ts209
1 files changed, 0 insertions, 209 deletions
diff --git a/frontend/app/api/fs/[...path]/route.ts b/frontend/app/api/fs/[...path]/route.ts
deleted file mode 100644
index 3a299af..0000000
--- a/frontend/app/api/fs/[...path]/route.ts
+++ /dev/null
@@ -1,209 +0,0 @@
1import { NextRequest, NextResponse } from 'next/server'
2import { writeFile, unlink } from 'fs/promises'
3import { tmpdir } from 'os'
4import { join } from 'path'
5import { randomUUID } from 'crypto'
6import { Auth_get_user, Auth_user_can_upload } from '@/lib/auth'
7import { Drive_ls, Drive_remove, Drive_mkdir, Drive_import } from '@/lib/drive_server'
8import { UPLOAD_MAX_FILE_SIZE } from '@/lib/constants'
9import { revalidatePath } from 'next/cache'
10
11// GET /api/fs/path/to/file - Get file/directory listing
12export async function GET(
13 request: NextRequest,
14 { params }: { params: Promise<{ path: string[] }> }
15) {
16 try {
17 const { path: pathSegments } = await params
18 const filePath = '/' + (pathSegments?.join('/') || '')
19
20 // Get directory listing using Drive_ls (non-recursive)
21 const entries = await Drive_ls(filePath, false)
22
23 return NextResponse.json(entries)
24
25 } catch (error) {
26 console.error('GET fs error:', error)
27 return NextResponse.json(
28 { error: error instanceof Error ? error.message : 'Internal server error' },
29 { status: 500 }
30 )
31 }
32}
33
34// DELETE /api/fs/path/to/file - Delete file/directory
35export async function DELETE(
36 request: NextRequest,
37 { params }: { params: Promise<{ path: string[] }> }
38) {
39 try {
40 // Check user authentication and permissions
41 const user = await Auth_get_user()
42 if (!user.isLoggedIn) {
43 return NextResponse.json({ error: 'User not authenticated' }, { status: 401 })
44 }
45
46 if (!Auth_user_can_upload(user)) {
47 return NextResponse.json({ error: 'User does not have upload permissions' }, { status: 403 })
48 }
49
50 const { path: pathSegments } = await params
51 const filePath = '/' + (pathSegments?.join('/') || '')
52
53 // Remove file/directory using Drive_remove
54 await Drive_remove(filePath, user.email)
55
56 // Revalidate the parent directory to refresh listings
57 const parentPath = filePath.split('/').slice(0, -1).join('/') || '/'
58 revalidatePath(`/drive${parentPath}`)
59 revalidatePath('/drive')
60
61 return NextResponse.json({
62 success: true,
63 message: 'Path deleted successfully',
64 deletedPath: filePath
65 })
66
67 } catch (error) {
68 console.error('DELETE fs error:', error)
69 return NextResponse.json(
70 { error: error instanceof Error ? error.message : 'Internal server error' },
71 { status: 500 }
72 )
73 }
74}
75
76// PUT /api/fs/path/to/file - Create/upload file
77export async function PUT(
78 request: NextRequest,
79 { params }: { params: Promise<{ path: string[] }> }
80) {
81 try {
82 // Check user authentication and permissions
83 const user = await Auth_get_user()
84 if (!user.isLoggedIn) {
85 return NextResponse.json({ error: 'User not authenticated' }, { status: 401 })
86 }
87
88 if (!Auth_user_can_upload(user)) {
89 return NextResponse.json({ error: 'User does not have upload permissions' }, { status: 403 })
90 }
91
92 const { path: pathSegments } = await params
93 const filePath = '/' + (pathSegments?.join('/') || '')
94
95 // Check if request has file content
96 const contentType = request.headers.get('content-type')
97 if (!contentType || (!contentType.includes('multipart/form-data') && !contentType.includes('application/octet-stream'))) {
98 return NextResponse.json({
99 error: 'Content-Type must be multipart/form-data or application/octet-stream'
100 }, { status: 400 })
101 }
102
103 let fileBuffer: Buffer
104 let filename: string
105
106 if (contentType.includes('multipart/form-data')) {
107 // Handle multipart form data
108 const formData = await request.formData()
109 const file = formData.get('file') as File
110
111 if (!file) {
112 return NextResponse.json({ error: 'No file provided' }, { status: 400 })
113 }
114
115 if (file.size > UPLOAD_MAX_FILE_SIZE) {
116 return NextResponse.json({
117 error: `File exceeds maximum size of ${UPLOAD_MAX_FILE_SIZE / (1024 * 1024)}MB`
118 }, { status: 400 })
119 }
120
121 const bytes = await file.arrayBuffer()
122 fileBuffer = Buffer.from(bytes)
123 filename = file.name
124 } else {
125 // Handle raw binary data
126 const bytes = await request.arrayBuffer()
127 fileBuffer = Buffer.from(bytes)
128
129 if (fileBuffer.length > UPLOAD_MAX_FILE_SIZE) {
130 return NextResponse.json({
131 error: `File exceeds maximum size of ${UPLOAD_MAX_FILE_SIZE / (1024 * 1024)}MB`
132 }, { status: 400 })
133 }
134
135 // Extract filename from path
136 filename = pathSegments?.[pathSegments.length - 1] || 'upload'
137 }
138
139 // Create temporary file
140 const tempFileName = `${randomUUID()}-${filename}`
141 const tempFilePath = join(tmpdir(), tempFileName)
142
143 // Save file to temporary location
144 await writeFile(tempFilePath, fileBuffer)
145
146 // Import file using Drive_import (uses --mode move, so temp file is already deleted)
147 await Drive_import(tempFilePath, filePath, user.email)
148
149 // Revalidate the parent directory to refresh listings
150 const parentPath = filePath.split('/').slice(0, -1).join('/') || '/'
151 revalidatePath(`/drive${parentPath}`)
152 revalidatePath('/drive')
153
154 return NextResponse.json({
155 success: true,
156 message: 'File uploaded successfully',
157 path: filePath
158 })
159
160 } catch (error) {
161 console.error('PUT fs error:', error)
162 return NextResponse.json(
163 { error: error instanceof Error ? error.message : 'Internal server error' },
164 { status: 500 }
165 )
166 }
167}
168
169// POST /api/fs/path/to/directory - Create directory
170export async function POST(
171 request: NextRequest,
172 { params }: { params: Promise<{ path: string[] }> }
173) {
174 try {
175 // Check user authentication and permissions
176 const user = await Auth_get_user()
177 if (!user.isLoggedIn) {
178 return NextResponse.json({ error: 'User not authenticated' }, { status: 401 })
179 }
180
181 if (!Auth_user_can_upload(user)) {
182 return NextResponse.json({ error: 'User does not have upload permissions' }, { status: 403 })
183 }
184
185 const { path: pathSegments } = await params
186 const dirPath = '/' + (pathSegments?.join('/') || '')
187
188 // Create directory using Drive_mkdir
189 await Drive_mkdir(dirPath, user.email)
190
191 // Revalidate the parent directory to refresh listings
192 const parentPath = dirPath.split('/').slice(0, -1).join('/') || '/'
193 revalidatePath(`/drive${parentPath}`)
194 revalidatePath('/drive')
195
196 return NextResponse.json({
197 success: true,
198 message: 'Directory created successfully',
199 path: dirPath
200 })
201
202 } catch (error) {
203 console.error('POST fs error:', error)
204 return NextResponse.json(
205 { error: error instanceof Error ? error.message : 'Internal server error' },
206 { status: 500 }
207 )
208 }
209} \ No newline at end of file